package com.liuqi.aiagent.util;

import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import lombok.Getter;

/**
 * @author liuqi
 * @date 2025/7/18
 **/
public final class ResponseUtil {

    /**
     * 响应错误消息
     *
     * @param response
     * @param message
     */
    public static void responseError(HttpServletResponse response, String message) {
        response.setStatus(500);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String errorMessage = JSONObject.toJSONString(new ErrorResponse(500, message));
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(errorMessage.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Getter
    public static class ErrorResponse {
        int status;
        String message;
        public ErrorResponse(int status, String message) {
            this.status = status;
            this.message = message;
        }
    }

}
